/*
 * JStock - Free Stock Market Software
 * Copyright (C) 2010 Yan Cheng CHEOK <yccheok@yahoo.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package org.yccheok.jstock.gui;

import java.awt.Color;
import java.awt.Cursor;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JLabel;
import org.yccheok.jstock.engine.Code;
import org.yccheok.jstock.engine.Country;
import org.yccheok.jstock.engine.Index;
import org.yccheok.jstock.engine.Market;
import org.yccheok.jstock.engine.Stock;
import org.yccheok.jstock.engine.Symbol;
import org.yccheok.jstock.internationalization.GUIBundle;

/**
 *
 * @author  yccheok
 */
public class MarketJPanel extends javax.swing.JPanel {

    /** Creates new form MarketJPanel */
    public MarketJPanel(Country country) {
        initComponents();
        
        this.country = country;        
        initAccordingToCountry(country);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        leftPanel = new javax.swing.JPanel();
        rightPanel = new javax.swing.JPanel();

        setLayout(new java.awt.BorderLayout());
        add(leftPanel, java.awt.BorderLayout.WEST);
        add(rightPanel, java.awt.BorderLayout.EAST);
    }// </editor-fold>//GEN-END:initComponents

    public void update(Market market) {
        List<Index> indices = org.yccheok.jstock.engine.Utils.getStockIndices(country);
        final java.text.NumberFormat numberFormat = java.text.NumberFormat.getInstance();
        
        map.get("volume").setText(numberFormat.format(market.getVolume()));
        map.get("up").setText(numberFormat.format(market.getNumOfStockChange(Market.ChangeType.Up)));
        map.get("down").setText(numberFormat.format(market.getNumOfStockChange(Market.ChangeType.Down)));
        map.get("unchanged").setText(numberFormat.format(market.getNumOfStockChange(Market.ChangeType.Unchange)));

        numberFormat.setMaximumFractionDigits(2);
        numberFormat.setMinimumFractionDigits(2);

        for (Index index : indices) {
            final double _index = market.getIndex(index);
            final double change = market.getChange(index);
            final Color color = Utils.getColor(change, 0.0);
            
            JLabel label = map.get(index.name());
            
            if (label == null) {
                continue;
            }
            
            label.setText(numberFormat.format(_index) + " (" + numberFormat.format(change) + ")");
            label.setForeground(color);
        }                
        
        map.get("value").setText(numberFormat.format(market.getValue()));
    }
    
    private void initAccordingToCountry(Country country) {
        List<Index> indices = org.yccheok.jstock.engine.Utils.getStockIndices(country);
        for (final Index index : indices) {
            JLabel name = new JLabel(index.toString() + " : ");
            leftPanel.add(name);
            JLabel value = new JLabel();
            value.setName(index.name());
            map.put(index.name(), value);
            value.setFont(Utils.getBoldFont(value.getFont()));
            leftPanel.add(value);

            // Install mouse handler.
            name.addMouseListener(new java.awt.event.MouseAdapter() {
                @Override
                public void mouseExited(java.awt.event.MouseEvent evt) {
                    MarketJPanel.this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                }

                @Override
                public void mouseEntered(java.awt.event.MouseEvent evt) {
                    MarketJPanel.this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                }

                @Override
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    final Code code = index.getCode();
                    final MainFrame m = MainFrame.getInstance();
                    final Stock stock = Utils.getEmptyStock(code, Symbol.newInstance(code.toString()));
                    m.displayHistoryChart(stock);
                }
            });
        }
        
        JLabel volume = new JLabel(java.util.ResourceBundle.getBundle("org/yccheok/jstock/data/gui").getString("MarketJPanel_Volume"));
        leftPanel.add(volume);
        JLabel volume_value = new JLabel();
        volume_value.setName("volume");
        map.put("volume", volume_value);
        volume_value.setFont(Utils.getBoldFont(volume_value.getFont()));
        volume_value.setForeground(new java.awt.Color(153, 102, 0));
        leftPanel.add(volume_value);

        JLabel value = new JLabel(this.getValueLabel());
        leftPanel.add(value);
        JLabel value_value = new JLabel();
        value_value.setName("value");
        map.put("value", value_value);
        value_value.setFont(Utils.getBoldFont(value_value.getFont()));
        value_value.setForeground(new java.awt.Color(153, 102, 0));
        leftPanel.add(value_value);
        
        JLabel up = new JLabel(java.util.ResourceBundle.getBundle("org/yccheok/jstock/data/gui").getString("MarketJPanel_Up"));
        rightPanel.add(up);
        JLabel up_value = new JLabel();
        up_value.setName("up");
        map.put("up", up_value);
        up_value.setFont(Utils.getBoldFont(up_value.getFont()));
        up_value.setForeground(new java.awt.Color(50, 150, 0));
        rightPanel.add(up_value);

        JLabel down = new JLabel(java.util.ResourceBundle.getBundle("org/yccheok/jstock/data/gui").getString("MarketJPanel_Down"));
        rightPanel.add(down);
        JLabel down_value = new JLabel();
        down_value.setName("down");
        map.put("down", down_value);
        down_value.setFont(Utils.getBoldFont(down_value.getFont()));
        down_value.setForeground(new java.awt.Color(200, 0, 50));
        rightPanel.add(down_value);

        JLabel unchanged = new JLabel(java.util.ResourceBundle.getBundle("org/yccheok/jstock/data/gui").getString("MarketJPanel_Unchanged"));
        rightPanel.add(unchanged);
        JLabel unchanged_value = new JLabel();
        unchanged_value.setName("unchanged");
        map.put("unchanged", unchanged_value);
        unchanged_value.setFont(Utils.getBoldFont(unchanged_value.getFont()));
        unchanged_value.setForeground(new java.awt.Color(0, 0, 0));
        rightPanel.add(unchanged_value);      
    }

    private String getValueLabel() {
        return MessageFormat.format(
            GUIBundle.getString("MainFrame_ValueLabel_template"),
            /*java.util.Currency.getInstance(Locale.getDefault()).getSymbol()*/"$"
        );
    }

    public Country getCountry() {
        return this.country;
    }
    
    private Country country = null;
    private Map<String, JLabel> map = new HashMap<String, JLabel>();
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel leftPanel;
    private javax.swing.JPanel rightPanel;
    // End of variables declaration//GEN-END:variables

}
